package com.atguigu.tingshu.dispatch.job;
import java.util.Date;

import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.dispatch.mapper.XxlJobLogMapper;
import com.atguigu.tingshu.search.client.SearchFeignClient;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.google.common.collect.Maps;

import com.atguigu.tingshu.model.dispatch.XxlJobLog;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class DispatchJobHandler {

    @Autowired
    private XxlJobLogMapper xxlJobLogMapper ;

    @Autowired
    private SearchFeignClient searchFeignClient ;

    @Autowired
    private UserInfoFeignClient userInfoFeignClient ;

    @XxlJob(value = "updateLatelyAlbumRankingJobHandler")
    public void updateLatelyAlbumRankingJob() {

        // 获取任务id
        long jobId = XxlJobHelper.getJobId();

        // 记录任务执行的日志
        XxlJobLog xxlJobLog = new XxlJobLog() ;
        xxlJobLog.setJobId(jobId);

        long startTime = System.currentTimeMillis();

        // 远程调用search微服务的更新排行榜数据接口
        Result result = searchFeignClient.updateLatelyAlbumRanking();

        long endTime = System.currentTimeMillis();

        Integer code = result.getCode();
        if(code.intValue() == 200) {
            xxlJobLog.setStatus(1);
        }else {
            xxlJobLog.setStatus(0);
            xxlJobLog.setError(result.getMessage());
        }

        xxlJobLog.setTimes((int)(endTime - startTime));
        xxlJobLogMapper.insert(xxlJobLog) ;


    }

    @XxlJob(value = "updateVipExpireStatusJobHandler")
    public void updateVipExpireStatusJob() {

        // 获取任务id
        long jobId = XxlJobHelper.getJobId();

        // 记录任务执行的日志
        XxlJobLog xxlJobLog = new XxlJobLog() ;
        xxlJobLog.setJobId(jobId);

        long startTime = System.currentTimeMillis();

        // 远程调用search微服务的更新排行榜数据接口
        Result result = userInfoFeignClient.updateVipExpireStatus() ;

        long endTime = System.currentTimeMillis();

        Integer code = result.getCode();
        if(code.intValue() == 200) {
            xxlJobLog.setStatus(1);
        }else {
            xxlJobLog.setStatus(0);
            xxlJobLog.setError(result.getMessage());
        }

        xxlJobLog.setTimes((int)(endTime - startTime));
        xxlJobLogMapper.insert(xxlJobLog) ;
    }

}




























